package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.EditorWishActivity;
import com.wangtao.firstapp.activity.OriginalAuthorActivity;
import com.wangtao.firstapp.activity.ShopDetailsActivity;
import com.wangtao.firstapp.custom.FullyLinearLayoutManager;
import com.wangtao.firstapp.custom.ScRecyclerView;
import com.wangtao.firstapp.modles.ThreeSifeSecond;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;


/**
 * 二级页面-->小唐君无图页面（主内容 适配器）
 * Created by lanouhn on 16/9/5.
 */
public class ThreeSecondContentAdapter extends RecyclerView.Adapter<ThreeSecondContentAdapter.ContentViewHolder> {

    //用于存作者Id
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    //用于存商品ID
    private SharedPreferences sp_shop;
    private SharedPreferences.Editor editor_shop;

    //评论
    private ThreeSecondReviewAdapter threeSecondReviewAdapter;

    //标志位
    private int flag=0;


    private Context context;
    private ThreeSifeSecond threeSifeSecond;


    public ThreeSecondContentAdapter(Context context,
                                     ThreeSifeSecond threeSifeSecond
    ) {
        this.context = context;
        this.threeSifeSecond = threeSifeSecond;
    }


    @Override
    public ContentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.act_sife_second_three_item_content, null);
        ContentViewHolder holder = new ContentViewHolder(view);

        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = sp.edit();

        sp_shop = context.getSharedPreferences("shopconfig", Context.MODE_PRIVATE);
        editor_shop = sp_shop.edit();
        return holder;
    }

    @Override
    public void onBindViewHolder(final ContentViewHolder holder, final int position) {
        final ThreeSifeSecond.DataBean dataBean = threeSifeSecond.getData();

        if (position == 0) {
            holder.linearLayoutOne.setVisibility(View.VISIBLE);
            holder.title.setText(dataBean.getTitle());
            Picasso.with(context).load(dataBean.getUser().getAvatar()).into(holder.logoOne);
            holder.author.setText(dataBean.getUser().getNickname());
            holder.views.setText(dataBean.getViews());
            holder.contentOne.setText(dataBean.getDesc());
        } else {
            int i = position - 1;
            holder.linearLayoutTwo.setVisibility(View.VISIBLE);
            holder.linearLayoutOne.setVisibility(View.GONE);
            holder.name.setText(dataBean.getPost_list().get(i).getUser().getNickname());
            holder.contentTwo.setText(dataBean.getPost_list().get(i).getContent());

            if (holder.tv_title1.getChildCount() > 0) {
                holder.tv_title1.removeAllViews();
            }

            TextView textView1 = new TextView(context);
            if (!dataBean.getPost_list().get(i).getBrand_product().get(0).getBrand().getName().equals("")) {
                textView1.setText(dataBean.getPost_list().get(i).getBrand_product().get(0).getBrand().getName() + " ");
            }
            TextView textView2 = new TextView(context);
            if (!dataBean.getPost_list().get(i).getBrand_product().get(0).getBrand().getAlias().equals("")) {
                textView2.setText(dataBean.getPost_list().get(i).getBrand_product().get(0).getBrand().getAlias() + " ");
            }
            TextView textView3 = new TextView(context);
            textView3.setText(dataBean.getPost_list().get(i).getBrand_product().get(0).getTitle());

            holder.tv_title1.addView(textView1);
            holder.tv_title1.addView(textView2);
            holder.tv_title1.addView(textView3);

            holder.price.setText(dataBean.getPost_list().get(i).getBrand_product().get(0).getPrice());
            holder.likes_tv.setText(dataBean.getPost_list().get(i).getDynamic().getLikes()+"人喜欢");
            Picasso.with(context).load(dataBean.getPost_list().get(i).getUser().getAvatar()).into(holder.logoTwo);

            if (holder.linearLayout_photo.getChildCount() > 0)
                holder.linearLayout_photo.removeAllViews();

            for (int j = 0; j < dataBean.getPost_list().get(i).getPics().size(); j++) {
                ImageView imageView = new ImageView(context);
                ImageOptions imageOptions = new ImageOptions.Builder()
                        .setSize(DensityUtil.dip2px(400), DensityUtil.dip2px(400))//图片大小
                        //.setRadius(DensityUtil.dip2px(5))//ImageView圆角半径
                        .setCrop(true)// 如果ImageView的大小不是定义为wrap_content, 不要crop.
                        .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                        //.setLoadingDrawableId(R.mipmap.ic_launcher)//加载中默认显示图片
                        //.setFailureDrawableId(R.mipmap.ic_launcher)//加载失败后默认显示图片
                        .build();

                imageView.setPadding(0, 0, 0, 15);
                x.image().bind(imageView, dataBean.getPost_list().get(i).getPics().get(j).getUrl(), imageOptions);
                holder.linearLayout_photo.addView(imageView);
            }
            Picasso.with(context).load(dataBean.getPost_list().get(i).getBrand_product().get(0).getPic()).into(holder.iv_shop);


            if (dataBean.getPost_list().get(i).getComments().size() > 0) {

                holder.linearLayout_Review_Review.setLayoutManager(new FullyLinearLayoutManager(context));

                threeSecondReviewAdapter = new ThreeSecondReviewAdapter(context,
                        dataBean.getPost_list().get(i).getComments(),dataBean.getPost_list().get(i).getId());
                holder.linearLayout_Review_Review.setAdapter(threeSecondReviewAdapter);

            }

        }

        //作者介绍页面--小糖君
        holder.author.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editor.putString("author_iD", "1");
                editor.commit();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                context.startActivity(intent);
                // Toast.makeText(context, "作者介绍页面--小糖君", Toast.LENGTH_SHORT).show();
            }
        });

        holder.logoOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editor.putString("author_iD", "1");
                editor.commit();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                context.startActivity(intent);
                // Toast.makeText(context, "作者介绍页面--小糖君", Toast.LENGTH_SHORT).show();
            }
        });

        //用户作者 介绍页面（Head 喜欢，关注，粉丝，，）
        //作者介绍页面--用户作者
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 获取作者ID
                String author_ID = threeSifeSecond.getData().getPost_list().get(position - 1).getUser().getUser_id();
                editor.putString("author_iD", author_ID);
                editor.commit();

                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                context.startActivity(intent);
            }
        });
        holder.logoTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //获取作者ID
                String author_ID = threeSifeSecond.getData().getPost_list().get(position-1).getUser().getUser_id();
                editor.putString("author_iD", author_ID);
                editor.commit();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                context.startActivity(intent);
            }
        });


        //点击后弹出一句话“关注成功”，同时点击区域背景变色
        holder.atention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.atention.setVisibility(View.GONE);
                holder.attention_two.setVisibility(View.VISIBLE);
                Toast.makeText(context, "关注成功", Toast.LENGTH_SHORT).show();
            }
        });

        holder.attention_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.atention.setVisibility(View.VISIBLE);
                holder.attention_two.setVisibility(View.GONE);
            }
        });


        //进入单品详情页面
        holder.re_gouwu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取商品ID
                String shop_ID = dataBean.getPost_list().get(position-1).getBrand_product().get(0).getId();
//                editor_shop.putString("shop_iD",shop_ID);
//                editor_shop.commit();
                Intent intent = new Intent(context, ShopDetailsActivity.class);
                intent.putExtra("shop_iD", shop_ID);
                context.startActivity(intent);
                //Toast.makeText(context, "单品详情页面", Toast.LENGTH_SHORT).show();
            }
        });

         // 第一次点击，收入心愿单。第二次，，弹出窗口
        holder.re_one_view_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag==0){
                    holder.textView.setVisibility(View.VISIBLE);
                    holder.imageView_like.setVisibility(View.VISIBLE);
                    holder.likes.setVisibility(View.GONE);
                    holder.tv_likes.setVisibility(View.GONE);
                    flag=1;
                    //toastUtils.toast(dataBean.getPost_list().get(position-1).getBrand_product().get(0).getPic(), 1000);
                    Toast toast = new Toast(context);
                    View view1 = LayoutInflater.from(context).inflate(R.layout.likes_wish_toast, null);
                    ImageView logoPhoto = (ImageView) view1.findViewById(R.id.logoPhoto_likes_wish_toast);
                    Picasso.with(context).load(dataBean.getPost_list().get(position-1)
                            .getBrand_product().get(0).getPic()).into(logoPhoto);
                    toast.setGravity(Gravity.BOTTOM, 0, 20);
                    toast.setView(view1);
                    toast.show();

                }else {

                    AlertDialog.Builder aBuilder= new AlertDialog.Builder(context);
                    View view = LayoutInflater.from(context).inflate(R.layout.likes_wish_dialog, null);
                    aBuilder.setView(view);
                    aBuilder.setCancelable(false);
                    final AlertDialog alertDialog = aBuilder.create();

                    ImageView bad = (ImageView) view.findViewById(R.id.bad_likes_wish_dialog);//右上角的“X”，点击退出Dialog
                    //容器，放置“我的喜欢”。点击后退出Dialog,toast
                    LinearLayout lltLike = (LinearLayout) view.findViewById(R.id.lltLike_likes_wish_dialog);
                    Button cancel = (Button) view.findViewById(R.id.cancel_likes_wish_dialog);//点击后，取消喜欢，并退出Dialog
                    TextView found = (TextView) view.findViewById(R.id.found_likes_wish_dialog);//点击后，跳转至 编辑心愿单页面
                    ImageView shopPhoto = (ImageView) view.findViewById(R.id.shopPhoto_likes_wish_dialog);//商品图片，获取显示
                    Picasso.with(context).load(dataBean.getPost_list().get(position-1)
                            .getBrand_product().get(0).getPic()).into(shopPhoto);

                    //退出Dialog
                    bad.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            alertDialog.cancel();
                        }
                    });

                    //退出Dialog，在Activity中写取消喜欢
                    cancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            holder.textView.setVisibility(View.GONE);
                            holder.imageView_like.setVisibility(View.GONE);
                            holder.likes.setVisibility(View.VISIBLE);
                            holder.tv_likes.setVisibility(View.VISIBLE);
                            flag = 0;
                            alertDialog.cancel();
                        }
                    });

                    //退出Dialog，并Toast
                    lltLike.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            Toast toast = new Toast(context);
                            View view1 = LayoutInflater.from(context).inflate(R.layout.likes_wish_toast, null);
                            ImageView logoPhoto = (ImageView) view1.findViewById(R.id.logoPhoto_likes_wish_toast);
                            Picasso.with(context).load(dataBean.getPost_list().get(position-1)
                                    .getBrand_product().get(0).getPic()).into(logoPhoto);
                            toast.setGravity(Gravity.BOTTOM, 0, 20);
                            toast.setView(view1);
                            toast.show();

                            alertDialog.cancel();
                        }
                    });

                    //跳转至，编辑心愿单页面
                    found.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            Intent intent = new Intent(context, EditorWishActivity.class);
                            context.startActivity(intent);

                        }
                    });
                    alertDialog.show();
                }
            }
        });

        //宝贝详情-- 进入淘宝页面
        holder.linearLayout_three_content_view_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               String urlAlter= dataBean.getPost_list().get(position-1).getBrand_product().get(0).getUrl();
                Uri uriTb = Uri.parse(urlAlter);
                Intent intent = new Intent(Intent.ACTION_VIEW, uriTb);
                context.startActivity(intent);

                //Toast.makeText(context, "宝贝详情-- 进入淘宝页面", Toast.LENGTH_SHORT).show();
            }
        });

//        //点击“我来说两句”，弹出窗口
//        holder.spake.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(context, "弹出评论窗口", Toast.LENGTH_SHORT).show();
//            }
//        });
    }


    @Override
    public int getItemCount() {
        return threeSifeSecond.getData().getPost_list().size() + 1;
    }

    public class ContentViewHolder extends RecyclerView.ViewHolder {

        private TextView title, author, views, contentOne, name, atention, attention_two, contentTwo, spake, price, likes_tv, textView, tv_likes;
        private ImageView logoOne, logoTwo, myselfPhoto, iv_shop;
        private LinearLayout linearLayoutOne, linearLayoutTwo, tv_title1,
                linearLayout_one_view_one, linearLayout_three_content_view_one, linearLayout_photo;
        private RelativeLayout re_one_view_one, re_gouwu;
        private ScRecyclerView linearLayout_Review_Review;
        private ImageButton likes, imageView_like;

        public ContentViewHolder(View itemView) {
            super(itemView);

            title = (TextView) itemView.findViewById(R.id.title_three_item_content);
            author = (TextView) itemView.findViewById(R.id.author_three_item_content);
            views = (TextView) itemView.findViewById(R.id.views_three_item_content);
            contentOne = (TextView) itemView.findViewById(R.id.contentOne_three_item_content);
            name = (TextView) itemView.findViewById(R.id.name_three_item_content);

            atention = (TextView) itemView.findViewById(R.id.atention_three_item_content);
            attention_two = (TextView) itemView.findViewById(R.id.atention_two_three_item_content);

            contentTwo = (TextView) itemView.findViewById(R.id.contentTwo_three_item_content);

            spake = (TextView) itemView.findViewById(R.id.spake_three_item_content);
            tv_title1 = (LinearLayout) itemView.findViewById(R.id.tv1_three_item_content);
            price = (TextView) itemView.findViewById(R.id.price_three_item_content);
            likes_tv = (TextView) itemView.findViewById(R.id.likes_three_item_content_act);

            logoOne = (ImageView) itemView.findViewById(R.id.logoOne_three_item_content);
            logoTwo = (ImageView) itemView.findViewById(R.id.logoTwo_three_item_content);
            myselfPhoto = (ImageView) itemView.findViewById(R.id.myself_three_item_content);
            iv_shop = (ImageView) itemView.findViewById(R.id.ivShop_three_item_content);

            //点击后显示的
            textView = (TextView) itemView.findViewById(R.id.textView_three_item_content);
            imageView_like = (ImageButton) itemView.findViewById(R.id.imageview_three_item_content_like);

            //现在显示的

            tv_likes = (TextView) itemView.findViewById(R.id.likes_three_item_content);
            likes = (ImageButton) itemView.findViewById(R.id.likes_three_item_content_view);

            linearLayout_Review_Review = (ScRecyclerView) itemView.findViewById(R.id.linearLayout_Review_Review);
            linearLayout_photo = (LinearLayout) itemView.findViewById(R.id.linearLayout_photo_three_item_content);
            linearLayoutOne = (LinearLayout) itemView.findViewById(R.id.linearLayout_one_three_item_content);
            linearLayoutTwo = (LinearLayout) itemView.findViewById(R.id.linearLayout_two_three_item_content);
            linearLayout_three_content_view_one = (LinearLayout) itemView.findViewById(R.id.linearLayout_three_content_view_one);
            re_one_view_one = (RelativeLayout) itemView.findViewById(R.id.relativeLayout_three_content_view_one);
            re_gouwu = (RelativeLayout) itemView.findViewById(R.id.re_gouwu);
        }
    }




}
